package com.huitone.gddw.service.impl.monitoring.base;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.ItemKeyDef;
import com.huitone.gddw.dao.entity.ObjItemProperty;
import com.huitone.gddw.dao.monitoring.IItemKeyDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.ItemKeyDefDTO;
import com.huitone.gddw.service.monitoring.base.IHistoryIntService;
import com.huitone.gddw.service.monitoring.base.IHistoryService;
import com.huitone.gddw.service.monitoring.base.IHistoryStrService;
import com.huitone.gddw.service.monitoring.base.IInnormalEventService;
import com.huitone.gddw.service.monitoring.base.IItemKeyDefService;
import com.huitone.gddw.service.monitoring.base.ITriggerService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 对象子项属性管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class ItemKeyDefService implements IItemKeyDefService {

	@Autowired
	private IItemKeyDefDao itemKeyDefDao;
	
	@Autowired
	private IHistoryService historyService;
	@Autowired
	private IHistoryIntService historyIntService;
	@Autowired
	private IHistoryStrService historyStrService;
	@Autowired
	private ITriggerService triggerService;
	@Autowired
	private IInnormalEventService innormalEventService;
	
	@Override
	public QueryPage<ItemKeyDefDTO> selectPage(QueryPage<ItemKeyDef> page) {
		Map<String, Object> params = page.getParams();
		params.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		params.put("end", page.getPageNo() * page.getPageSize());
		
		List<ItemKeyDefDTO> result = itemKeyDefDao.selectPage(params);
		int totalCount = itemKeyDefDao.selectCount(params);
		
		QueryPage<ItemKeyDefDTO> queryPage = new QueryPage<ItemKeyDefDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}
	
	@Override
	public ItemKeyDef getById(String itemId) {
		return itemKeyDefDao.getById(itemId);
	}
	
	@Override
	public List<ObjItemProperty> getByItemId(String itemId) {
		return itemKeyDefDao.getByItemId(itemId);
	}

	@Override
	public void delByObjId(String objId) {
		itemKeyDefDao.delByObjId(objId);
	}

	@Override
	public void delByItemId(String itemId) {
		itemKeyDefDao.delByItemId(itemId);
	}

	@Override
	public ItemKeyDefDTO findOne(String keyId) {
		return itemKeyDefDao.findOne(keyId);
	}

	@Override
	public void save(ItemKeyDef entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		itemKeyDefDao.save(entity);
	}

	@Override
	public void update(ItemKeyDef entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		itemKeyDefDao.update(entity);
	}

	@Override
	public void delete(String keyId) {
		// 监控异常事件信息
		innormalEventService.delByKeyId(keyId);
		// 删除监控预警信息
		triggerService.delByKeyId(keyId);
		
		// 删除监控数据表信息
		historyService.delByKeyId(keyId);
		historyIntService.delByKeyId(keyId);
		historyStrService.delByKeyId(keyId);
		
		// 删除属性信息
		itemKeyDefDao.delByKeyId(keyId);
	}

}
